package com.example.meetingroomnew.controll;

import com.example.meetingroomnew.Dao.UserDao;
import com.example.meetingroomnew.domain.*;
import com.example.meetingroomnew.service.MeetingRoomService;
import com.example.meetingroomnew.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@CrossOrigin(origins = {"*","null"})
@RestController
@RequestMapping
public class requestControll {
    @Autowired
    MeetingRoomService meetingRoomService;
    @Autowired
    RequestService requestService;
    @Autowired
    UserDao userDao;
//    按照id查询预约信息
   @PostMapping("/info")
    public DataClass<ReservationPlus>  SelectBypeople(@RequestBody User nuser){
        User user = userDao.selectById(nuser.getId());
        int type = user.getType();
       List<Reservation> reservations;
//        判断类型
        if(type==User.ADMIN){
            reservations = requestService.selectAll();
        }else {
            reservations = requestService.selectBypeople(user.getId());
        }
        List<ReservationPlus> rs =new ArrayList<ReservationPlus>();
        for(int i=0;i<reservations.size();i++){
            MeetingRoom m=meetingRoomService.selectByid(reservations.get(i).getRoomId());
            ReservationPlus r1=new ReservationPlus(m,reservations.get(i));
            rs.add(r1);
        }
//       DataClass<Reservation> dataClass=new DataClass<Reservation>();
//        dataClass.setData(reservations);
        DataClass<ReservationPlus> dataClass=new DataClass<>();
        dataClass.setData(rs);
        return  dataClass;

    }
//    0是成功，1是失败
    @PostMapping("/user/delete")
    public int deleteByid(@RequestBody Reservation reservation){
        int flag=requestService.deleteByid(reservation.getId());
//        System.out.println();
        if (flag==0){
            return 0;
        }else return 1;
    }

    @PostMapping("/affirm")
    public int affirm(@RequestBody Reservation reservation){
       boolean flag=requestService.Insert(reservation);
       if(flag==true){
           return 1;
       }else {
           return 0;
       }

    }
    @PostMapping("/user/accept")
    public int accept(@RequestBody Reservation reservation){
        Reservation reservation1 = requestService.selectByid(reservation.getId());
        reservation1.setStatus(Reservation.ACCEPT);
        requestService.updateRequest(reservation1);
        return 1;
    }

    @PostMapping("/decline")
    public int reject(@RequestBody Reservation reservation){
        System.out.println(reservation);
        Reservation reservation1 = requestService.selectByid(reservation.getId());
        reservation1.setStatus(Reservation.REJECT);
        reservation1.setRejectionReason(reservation.getRejectionReason());
        requestService.updateRequest(reservation1);
        return 1;
    }
}
